home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / NumericSpinner.java < prev    next >
Text File  |  1998-09-04  |  7KB  |  222 lines

  1. package symantec.itools.awt.util.spinner;
  2.  
  3.  
  4. import java.beans.PropertyVetoException;
  5. import java.beans.PropertyChangeListener;
  6. import java.beans.VetoableChangeListener;
  7.  
  8.  
  9. //     02/15/97    RKM    Merged in change to set min and max as in the DESC file
  10. //    06/03/97    MSH    Incorporated Java 1.1 changes
  11. //    06/03/97    LAB Polished update to Java 1.1.
  12. //                    Changed the package to symantec.itools.awt.util.spinner.
  13. //    10/06/97    LAB Updated constructor to set min, max, tempMin, and tempMax directly
  14. //                    rather than call their setters so it would get introspected properly,
  15. //                    since the setters only actually set after addNotify has been called.
  16. //  02/05/98    DS  added changes for Spinner re-write
  17. //  09/04/98    MSH Fixed editable support, #62836
  18.  
  19. /**
  20.  * Creates a text box, containing a list of numbers, with up and down arrows.
  21.  * Use this component to allow your users to move through a set of fixed
  22.  * values or type a valid value in the box.
  23.  * <p>
  24.  * At run time only the selected value is displayed in the text box.
  25.  * <p>
  26.  * @see symantec.itools.awt.Spinner
  27.  * @version 1.1, June 2, 1997
  28.  * @author Symantec
  29.  */
  30. public class NumericSpinner
  31.     extends    Spinner
  32.     implements java.io.Serializable
  33. {
  34.     /**
  35.      * Constructs an empty NumericSpinner.
  36.      */
  37.     public NumericSpinner()
  38.     {
  39.         try
  40.         {
  41.             setIncrement(1);
  42.         }
  43.         catch( PropertyVetoException e ){}
  44.         min        = 0;
  45.         max        = 10;
  46.     }
  47.  
  48.     /**
  49.      * Sets the minimum value the spinner may have.
  50.      *
  51.      * Overriden here to set the size of the text area.
  52.      *
  53.      * @param i the new minimum value
  54.      * @see Spinner#getMin
  55.      * @see #setMax
  56.      * @exception PropertyVetoException
  57.      * if the specified property value is unacceptable
  58.      */
  59.     public void setMin(int i)  throws PropertyVetoException
  60.     {
  61.         super.setMin(i);
  62.  
  63.         if(added)
  64.         {
  65.             int oldValue = textWidth;
  66.  
  67.             textWidth = Math.max(Integer.toString(min).length(), Integer.toString(max).length());
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Sets the maximum value the spinner may have.
  73.      *
  74.      * Overridden here to set the size of the text area.
  75.      *
  76.      * @param i the new maximum value
  77.      * @see Spinner#getMax
  78.      * @see #setMin
  79.      * @exception PropertyVetoException
  80.      * if the specified property value is unacceptable
  81.      */
  82.     public void setMax(int i) throws PropertyVetoException
  83.     {
  84.         super.setMax(i);
  85.  
  86.         if(added)
  87.         {
  88.             int oldValue = textWidth;
  89.  
  90.             textWidth = Math.max(Integer.toString(min).length(), Integer.toString(max).length());
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Sets the value to increment/decrement the Spinner by.
  96.      * @param int i the increment/decrement value
  97.      * @see #getIncrement
  98.      *
  99.      * @exception PropertyVetoException
  100.      * if the specified property value is unacceptable
  101.      */
  102.     public void setIncrement(int i) throws PropertyVetoException
  103.     {
  104.         if (increment != i)
  105.         {
  106.             Integer oldValue = new Integer( increment );
  107.             Integer newValue = new Integer( i );
  108.  
  109.             vetos.fireVetoableChange("increment", oldValue, newValue);
  110.             increment = i;
  111.             changes.firePropertyChange("increment", oldValue, newValue);
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Gets the increment/decrement value.
  117.      * @return the increment/decrement value
  118.      * @see #setIncrement
  119.      */
  120.     public int getIncrement()
  121.     {
  122.         return increment;
  123.     }
  124.  
  125.     /**
  126.      * Gets the current text from the Spinner.
  127.      * @return the text of the currently selected Spinner value
  128.      */
  129.     public String getCurrentText()
  130.     {
  131.         return Integer.toString(current);
  132.     }
  133.  
  134.     /**
  135.      * Tells this component that it has been added to a container.
  136.      * This is a standard Java AWT method which gets called by the AWT when
  137.      * this component is added to a container. Typically, it is used to
  138.      * create this component's peer.
  139.      * Here it's used to set maximum text width and note the text of the
  140.      * current selection.
  141.      *
  142.      * @see java.awt.Container#removeNotify
  143.      */
  144.     public void addNotify()
  145.     {
  146.         textWidth = Math.max(Integer.toString(min).length(), Integer.toString(max).length());
  147.         text = Integer.toString(current);
  148.         super.addNotify();
  149.         updateText(false);
  150.     }
  151.  
  152.     /**
  153.      * Adds a listener for all property change events.
  154.      * @param listener the listener to add
  155.      * @see #removePropertyChangeListener
  156.      */
  157.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  158.     {
  159.         super.addPropertyChangeListener(listener);
  160.         changes.addPropertyChangeListener(listener);
  161.     }
  162.  
  163.     /**
  164.      * Removes a listener for all property change events.
  165.      * @param listener the listener to remove
  166.      * @see #addPropertyChangeListener
  167.      */
  168.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  169.     {
  170.         super.removePropertyChangeListener(listener);
  171.         changes.removePropertyChangeListener(listener);
  172.     }
  173.  
  174.     /**
  175.      * Adds a listener for all vetoable property change events.
  176.      * @param listener the listener to add
  177.      * @see #removeVetoableChangeListener
  178.      */
  179.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  180.     {
  181.          super.addVetoableChangeListener(listener);
  182.         vetos.addVetoableChangeListener(listener);
  183.     }
  184.  
  185.     /**
  186.      * Removes a listener for all vetoable property change events.
  187.      * @param listener the listener to remove
  188.      * @see #addVetoableChangeListener
  189.      */
  190.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  191.     {
  192.         super.removeVetoableChangeListener(listener);
  193.         vetos.removeVetoableChangeListener(listener);
  194.     }
  195.  
  196.     /**
  197.      * returns boolean if text in field is a valid entry
  198.      * @return true if text is valid
  199.      */
  200.     protected boolean validateText( )
  201.     {
  202.         boolean result = false;
  203.  
  204.         try
  205.         {
  206.             String  s = textFld.getText();
  207.             Integer i = new Integer( s );
  208.  
  209.             if ( i.intValue() >= min && (int) i.intValue() <= max )
  210.                 result = true;
  211.         }
  212.         catch ( Exception e )
  213.         {
  214.         }
  215.         return result;
  216.     }
  217.  
  218.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  219.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  220.  
  221. }
  222.